package jee.boot.module.bpm.delegated;

import com.google.common.collect.Lists;
import jee.boot.common.utils.StringUtils;
import jee.boot.entity.bpm.*;
import jee.boot.entity.bpm.BpmConstant;
import jee.boot.module.bpm.service.BusinessDataService;
import jee.boot.module.bpm.service.FormDefinitionService;
import jee.boot.entity.task.ExportModelImpl;
import jee.boot.entity.task.IExportField;
import jee.boot.entity.task.IExportModel;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Iterator;
import java.util.List;

public abstract class BasicDelegated {
    @Autowired
    protected BusinessDataService businessDataService;
    @Autowired
    protected FormDefinitionService formDefinitionService;
    /**
     * 从当前用户的权限配置中读取模型配置数据,找到需要导出的列配置
     *
     * @param businessData
     * @return
     */
    public IExportModel getExportModel(BusinessData businessData, String type) {
        FormDefinition definition = formDefinitionService.getCompleteDataByName(businessData.getDefinition());
        FormConfig config =definition.coverConfig();
        businessData.configDefinition(definition);
        mixInBusinessColumn(config,businessData);
        ExportModelImpl exportModel = new ExportModelImpl();
        String title = definition.getDescription();
        exportModel.setTitle(title);
        List<ColumnConfig> columnList = config.getColumnList();
        List<IExportField> fieldList = Lists.newArrayList();
        if (columnList != null && !columnList.isEmpty()) {
            columnList.forEach(field -> {
                BusinessColumn tableColumn = field.getColumn();
                String fieldKey=BpmConstant.SYS_NO;
                switch (type){
                    case "exp":
                        fieldKey=field.getExp();
                        break;
                    case "imp":
                        fieldKey=field.getImp();
                        break;
                    case "display":
                        fieldKey=field.getDisplay();
                        break;
                }
                if(StringUtils.equals(fieldKey, BpmConstant.SYS_YES)) {
                    fieldList.add(new IExportField() {
                        @Override
                        public String getTitle() {
                            return tableColumn.getComment();
                        }

                        @Override
                        public String getFieldName() {
                            return field.getFieldName();
                        }

                        @Override
                        public Class<?> getFieldType() {
                            return null;
                        }

                        @Override
                        public String getDictType() {
                            // 字典类型
                            return tableColumn.getDictionaryType();
                        }

                        @Override
                        public String getDictField() {
                            return null;
                        }

                        @Override
                        public String getFormat() {
                            return tableColumn.getFormat();
                        }
                    });
                }
            });
        }
        exportModel.setExportFieldList(fieldList);
        return exportModel;
    }

    /**
     * 混入数据实体中的列信息
     */
    public void mixInBusinessColumn(FormConfig formConfig,BusinessData businessData){
        List<ColumnConfig> columnList = formConfig.getColumnList();
        for(ColumnConfig columnConfig:columnList){
            BusinessColumn tableColumn = getTableColumn(businessData, columnConfig);
            columnConfig.setColumn(tableColumn);
        }
    }
    /**
     * 根据列配置,获取列定义
     *
     * @param businessData
     * @param columnConfig
     * @return
     */
    public BusinessColumn getTableColumn(BusinessData businessData, ColumnConfig columnConfig) {

        String fieldName = columnConfig.getFieldName();
        String[] split = StringUtils.split(fieldName, ".");
        Iterator<String> iterator = Lists.newArrayList(split).iterator();


        BusTableRel relation = businessData.getRelation();
        BusinessTable table = businessData.getBusinessTable();
        List<BusinessColumn> columnList = table.coverColumnList();
        List<BusTableRel> children = relation.getChildren();


        while (iterator.hasNext()) {
            String current = iterator.next();
            if (iterator.hasNext()) {
                // 如果还有下一个节点,则应该在关联表中查找关系
                for (BusTableRel rel : children) {
                    if (StringUtils.equals(current, businessData.getJoinTableAlias(rel.getFks()))) {
                        relation = rel;
                        table = rel.getBusinessTable();
                        columnList = table.coverColumnList();
                        children = relation.getChildren();
                        continue;
                    }
                }
            } else {
                //如果没有下一个节点,则应在当前表属性中找
                return getColumnByFieldName(columnList, current);
            }

        }
        return null;
    }


    public BusinessColumn getColumnByFieldName(List<BusinessColumn> columnList, String fieldName) {
        for (BusinessColumn column : columnList) {
            if (StringUtils.equals(fieldName, column.getFieldName())) {
                return column;
            }
        }
        return null;
    }
}
